using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThrownPool : MonoBehaviour, Pool
{
    private List<GameObject> ObjectPool = new List<GameObject>();
    private GameObject PrefabOBJ;
    public PoolType PoolType
    {
        get
        {
            return PoolType.Thrown;
        }
        private set { }
    }

    public void Clear()
    {
        foreach (GameObject obj in ObjectPool)
        {
            DestroyImmediate(obj);
        }
        ObjectPool.Clear();
    }

    public void Free(GameObject obj)
    {
        if (ObjectPool.Contains(obj))
        {
            Debug.LogError("重复添加对象");
            return;
        }
        else
        {
            obj.transform.SetParent(this.transform);
            obj.SetActive(false);
            ObjectPool.Add(obj);
        }
    }
    public GameObject Obtain()
    {
        if (ObjectPool.Count > 0)
        {
            GameObject temp = ObjectPool[0];
            ObjectPool.RemoveAt(0);
            Reset(temp);
            return temp;
        }
        if (PrefabOBJ == null)
        {
            Debug.LogError("池预制体为空");
            return null;
        }
        GameObject newObj = GameObject.Instantiate(PrefabOBJ);
        Reset(newObj);
        return newObj;
    }

    public void Reset(GameObject obj)
    {
        obj.SetActive(true);
        obj.transform.position = Vector3.zero;
        obj.transform.rotation = Quaternion.Euler(Vector3.zero);
    }
    public void Start()
    {
        PrefabOBJ = ResourceManager.Instance.LoadRes<GameObject>("bullets/Shuriken_0");
    }
}
